//
//  gushixiangqingVC.m
//  qulvyou
//
//  Created by mmyy on 15/10/22.
//  Copyright (c) 2015年 mmyy. All rights reserved.
//

#import "gushixiangqingVC.h"
#import "gushixiangqing.h"
#import "gushixiangqingCell.h"
#import "userInfoVC.h"
#import "gushixiangqingUser.h"
#import "HPGrowingTextView.h"
@interface gushixiangqingVC ()<UITableViewDataSource, UITableViewDelegate, HPGrowingTextViewDelegate>

@end

@implementation gushixiangqingVC
{
    UITableView *_tableView;
    NSMutableArray *_dataSource;
    NSString *titleText;
    NSString *userId;
    NSMutableArray *_footData;
    UIView *_pinglunView;
    HPGrowingTextView *_hpTextView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.title = @"故事详情";
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    _footData = [NSMutableArray arrayWithCapacity:0];
    _dataSource = [NSMutableArray arrayWithCapacity:0];
    [Funcation getWithURL:[NSString stringWithFormat:gushiXiangqingUrl, self.stop_id] success:^(id responseObject) {
        NSDictionary *data = [responseObject objectForKey:@"data"];
        NSDictionary *spot = [data objectForKey:@"spot"];
        NSArray *detail_list = [spot objectForKey:@"detail_list"];
        userId = [[[data objectForKey:@"trip"] objectForKey:@"user"] objectForKey:@"id"];
        for (NSDictionary *dict in detail_list) {
            gushixiangqing *items = [gushixiangqing gushuxiangqingWithDict:dict];
            [_dataSource addObject:items];
        }
        titleText = [spot objectForKey:@"text"];
        NSArray *comments = [spot objectForKey:@"comments"];
        for (NSDictionary *comm in comments) {
            gushixiangqingUser *user = [gushixiangqingUser gushixiangqingUserWithDict:comm];
            [_footData addObject:user];
        }
        [self createTableView];
        
    } andFail:^(id error) {
        NSLog(@"fail");
    }];

}
-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorColor = [UIColor clearColor];
    _tableView.allowsSelection = NO;
    [self createHeadView];
    [self createFootView];
    [self.view addSubview:_tableView];
    
}
-(void)createHeadView
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(15, 15, self.view.frame.size.width - 30, 150)];
    UIImageView *photos = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 50, 50)];
    [photos setImageWithURL:[NSURL URLWithString:self.photo]];
    photos.layer.cornerRadius = 25;
    photos.layer.masksToBounds = YES;
    [headView addSubview:photos];
    photos.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    [photos addGestureRecognizer:tap];
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(50 + 20, 30, 150, 20)];
    name.text = self.user_name;
    name.font = [UIFont boldSystemFontOfSize:18];
    name.textColor = [UIColor blackColor];
    name.backgroundColor = [UIColor clearColor];
    [headView addSubview:name];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, 75, self.view.frame.size.width - 30, 2)];
    lineView.backgroundColor = [UIColor blackColor];
    [headView addSubview:lineView];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:18],
                              NSFontAttributeName,
                              paragraphStyle,
                              NSParagraphStyleAttributeName,
                              nil];
    CGRect expectedRect = [titleText boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
    CGSize expectedSize = expectedRect.size;
    
    UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(15, 85, self.view.frame.size.width - 30, expectedSize.height)];
    text.text = titleText;
    text.lineBreakMode = NSLineBreakByWordWrapping;
    text.numberOfLines = 0;
    text.font = [UIFont boldSystemFontOfSize:18];
    text.textColor = [UIColor blackColor];
    [headView addSubview:text];
    
    headView.frame = CGRectMake(0, 0, self.view.frame.size.width, 85 + expectedSize.height);
    _tableView.tableHeaderView = headView;
    

}
-(void)createFootView
{
    UIView *footView = [[UIView alloc] init];
    CGFloat footH = 0;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width - 10, 20)];
    label.text = [NSString stringWithFormat:@"---------%lu人参与评论-----------",(unsigned long)_footData.count];
    label.textColor = [UIColor blackColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18];
    [footView addSubview:label];
    footH += 25;
    for (int i = 0; i < _footData.count; i++) {
        gushixiangqingUser *item = [_footData objectAtIndex:i];
        UIImageView *avatar_l = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5 + footH + 5, 40, 40)];
        avatar_l.layer.masksToBounds = YES;
        avatar_l.layer.cornerRadius = 20;
        [avatar_l setImageWithURL:[NSURL URLWithString:item.avatar_l]];
        [footView addSubview:avatar_l];
        NSString *nameComme = [NSString stringWithFormat:@"%@:%@",item.name, item.comment];
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:nameComme];
        [attString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, [item.name length])];
        [attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange([item.name length] + 1, [nameComme length] - [item.name length] - 1)];
        [attString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, [nameComme length])];
        UILabel *name = [UILabel new];
        name.attributedText = attString;
        name.lineBreakMode = NSLineBreakByWordWrapping;
        name.numberOfLines = 0;
        name.backgroundColor = [UIColor clearColor];
        name.adjustsFontSizeToFitWidth = YES;
        [footView addSubview:name];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        
        NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10],
                                  NSFontAttributeName,
                                  paragraphStyle,
                                  NSParagraphStyleAttributeName,
                                  nil];
        CGRect expectedRect = [name.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 85, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
        CGSize expectedSize = expectedRect.size;
        
        [name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(footView.left).offset(55);
            make.top.equalTo(footView.top).offset(5 + footH + 20);
            make.width.equalTo(self.view.frame.size.width - 85);
            make.height.equalTo(expectedSize.height);
            
        }];
        footH += (expectedSize.height + 15)>40?expectedSize.height + 15:45;
        
    }
    footView.frame = CGRectMake(10, 10, self.view.frame.size.width - 10, footH);
    _tableView.tableFooterView = footView;
    
}
-(void)tapClick
{
    userInfoVC *vc = [[userInfoVC alloc] init];
    vc.userId = userId;
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ident = @"basis_cell";
    gushixiangqingCell *cell = (gushixiangqingCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[gushixiangqingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
    }
    cell.backgroundColor = [UIColor clearColor];
    gushixiangqing *items = [_dataSource objectAtIndex:indexPath.row];
    CGFloat PstartW = [[items photo_width] floatValue];
    CGFloat PnowW = self.view.frame.size.width - 30;
    CGFloat PstartH = [[items photo_height] floatValue];
    CGFloat PnowH = PstartH / (PstartW / PnowW);
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],
                              NSFontAttributeName,
                              paragraphStyle,
                              NSParagraphStyleAttributeName,
                              nil];
    CGRect expectedRect = [items.text boundingRectWithSize:CGSizeMake(PnowW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
    CGSize expectedSize = expectedRect.size;

    [cell.photo setImageWithURL:[NSURL URLWithString:items.photo] placeholderImage:[UIImage imageNamed:@"define"]];
    [cell.photo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.top).offset(20);
        make.left.equalTo(cell.left).offset(15);
        make.width.equalTo(PnowW);
        make.height.equalTo(PnowH);
    }];
    cell.text.text = items.text;
    [cell.text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.photo.bottom).offset(5);
        make.left.equalTo(cell.left).offset(15);
        make.width.equalTo(PnowW);
        make.height.equalTo(expectedSize.height);
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    gushixiangqing *items = [_dataSource objectAtIndex:indexPath.row];
    CGFloat PstartW = [[items photo_width] floatValue];
    CGFloat PnowW = self.view.frame.size.width - 30;
    CGFloat PstartH = [[items photo_height] floatValue];
    CGFloat PnowH = PstartH / (PstartW / PnowW);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    
    NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],
                              NSFontAttributeName,
                              paragraphStyle,
                              NSParagraphStyleAttributeName,
                              nil];
    CGRect expectedRect = [items.text boundingRectWithSize:CGSizeMake(PnowW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
    CGSize expectedSize = expectedRect.size;

    return PnowH + expectedSize.height + 20 + 5;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
